package huawei.widget;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import huawei.android.widget.DecouplingUtil.ReflectUtil;
import java.util.HashMap;
import o.bt;
import o.fo;
import o.fpt;
import o.fpu;

public class HwListPopupWindow implements bt {
    private int ʻ;
    private int ʻॱ;
    private boolean ʼ;
    private final a ʼॱ;
    private int ʽ;
    private final b ʽॱ;
    private final d ʾ;
    private OnItemClickListener ʿ;
    private HashMap<Integer, Drawable> ˈ;
    private final Rect ˉ;
    private int ˊ;
    private final c ˊˊ;
    private Rect ˊˋ;
    private boolean ˊॱ;
    private Runnable ˊᐝ;
    private Context ˋ;
    private final Handler ˋˊ;
    private int ˋॱ;
    private boolean ˋᐝ;
    private PopupWindow ˍ;
    private fpt ˎ;
    private int ˏ;
    private int ˏॱ;
    private boolean ͺ;
    private ListAdapter ॱ;
    private boolean ॱˊ;
    private Drawable ॱˋ;
    private View ॱˎ;
    private boolean ॱॱ;
    private DataSetObserver ॱᐝ;
    private int ᐝ;
    private View ᐝॱ;

    class AnonymousClass4 extends fpu {
        final /* synthetic */ HwListPopupWindow ॱ;

        public bt ˏ() {
            return this.ॱ;
        }
    }

    class a implements OnTouchListener {
        final /* synthetic */ HwListPopupWindow ॱ;

        private a(HwListPopupWindow hwListPopupWindow) {
            this.ॱ = hwListPopupWindow;
        }

        public boolean onTouch(View view, MotionEvent motionEvent) {
            int action = motionEvent.getAction();
            int x = (int) motionEvent.getX();
            int y = (int) motionEvent.getY();
            boolean z = action == 0 && this.ॱ.ˍ != null && this.ॱ.ˍ.isShowing();
            boolean z2;
            if (x < 0 || x >= this.ॱ.ˍ.getWidth() || y < 0 || y >= this.ॱ.ˍ.getHeight()) {
                z2 = false;
            } else {
                z2 = true;
            }
            if (z && r0) {
                this.ॱ.ˋˊ.postDelayed(this.ॱ.ʽॱ, 250);
            }
            if (action == 1) {
                this.ॱ.ˋˊ.removeCallbacks(this.ॱ.ʽॱ);
            }
            return false;
        }
    }

    class b implements Runnable {
        final /* synthetic */ HwListPopupWindow ˊ;

        private b(HwListPopupWindow hwListPopupWindow) {
            this.ˊ = hwListPopupWindow;
        }

        public void run() {
            int i;
            int i2 = 1;
            int i3 = (this.ˊ.ˎ == null || !this.ˊ.ˎ.isAttachedToWindow()) ? 0 : 1;
            if (this.ˊ.ˎ.getCount() > this.ˊ.ˎ.getChildCount()) {
                i = 1;
            } else {
                i = 0;
            }
            i3 &= i;
            if (this.ˊ.ˎ.getChildCount() > this.ˊ.ˋॱ) {
                i2 = 0;
            }
            if ((i3 & i2) != 0) {
                this.ˊ.ˍ.setInputMethodMode(2);
                this.ˊ.ˊ();
            }
        }
    }

    class c implements Runnable {
        final /* synthetic */ HwListPopupWindow ˎ;

        private c(HwListPopupWindow hwListPopupWindow) {
            this.ˎ = hwListPopupWindow;
        }

        public void run() {
            this.ˎ.ʽ();
        }
    }

    class d implements OnScrollListener {
        final /* synthetic */ HwListPopupWindow ˋ;

        private d(HwListPopupWindow hwListPopupWindow) {
            this.ˋ = hwListPopupWindow;
        }

        public void onScroll(AbsListView absListView, int i, int i2, int i3) {
        }

        public void onScrollStateChanged(AbsListView absListView, int i) {
            if (i == 1 && this.ˋ.ˏॱ() && this.ˋ.ˍ.getContentView() != null) {
                this.ˋ.ˋˊ.removeCallbacks(this.ˋ.ʽॱ);
                this.ˋ.ʽॱ.run();
            }
        }
    }

    class e extends DataSetObserver {
        final /* synthetic */ HwListPopupWindow ˋ;

        private e(HwListPopupWindow hwListPopupWindow) {
            this.ˋ = hwListPopupWindow;
        }

        public void onChanged() {
            if (this.ˋ.ˎ()) {
                this.ˋ.ˊ();
            }
        }

        public void onInvalidated() {
            this.ˋ.ˋ();
        }
    }

    @Nullable
    public /* synthetic */ ListView ᐝ() {
        return ˊॱ();
    }

    public HwListPopupWindow(@NonNull Context context, @Nullable AttributeSet attributeSet) {
        this(context, attributeSet, 16843519, 0);
    }

    public HwListPopupWindow(@NonNull Context context, @Nullable AttributeSet attributeSet, @AttrRes int i) {
        this(context, attributeSet, i, 0);
    }

    public HwListPopupWindow(@NonNull Context context, @Nullable AttributeSet attributeSet, @AttrRes int i, @StyleRes int i2) {
        this.ˊ = -2;
        this.ˏ = -2;
        this.ʽ = 1002;
        this.ॱॱ = false;
        this.ʼ = false;
        this.ˊॱ = false;
        this.ˏॱ = 0;
        this.ͺ = false;
        this.ॱˊ = false;
        this.ˋॱ = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
        this.ʻॱ = 0;
        this.ˈ = new HashMap(4);
        this.ʽॱ = new b();
        this.ʼॱ = new a();
        this.ʾ = new d();
        this.ˊˊ = new c();
        this.ˉ = new Rect();
        this.ˋᐝ = false;
        this.ˋ = context;
        this.ˋˊ = new Handler(context.getMainLooper());
        this.ᐝ = 0;
        this.ʻ = 0;
        if (this.ʻ != 0) {
            this.ॱॱ = true;
        }
        this.ˍ = new PopupWindow(context, attributeSet, i, i2);
        this.ˍ.setInputMethodMode(1);
    }

    public void ˎ(@Nullable ListAdapter listAdapter) {
        if (this.ॱᐝ == null) {
            this.ॱᐝ = new e();
        }
        if (this.ॱ != null) {
            this.ॱ.unregisterDataSetObserver(this.ॱᐝ);
        }
        this.ॱ = listAdapter;
        if (this.ॱ != null) {
            listAdapter.registerDataSetObserver(this.ॱᐝ);
        }
        if (this.ˎ != null) {
            this.ˎ.setAdapter(this.ॱ);
        }
    }

    public void ˏ(int i) {
        this.ʻॱ = i;
    }

    public void ˎ(boolean z) {
        this.ˋᐝ = z;
        this.ˍ.setFocusable(z);
    }

    public void ˊ(Drawable drawable) {
        this.ॱˋ = drawable;
    }

    public void ˊ(Drawable drawable, int i) {
        if (drawable != null) {
            this.ˈ.put(Integer.valueOf(i), drawable);
        }
    }

    @Nullable
    public Drawable ˏ() {
        return this.ˍ.getBackground();
    }

    public void ˎ(@Nullable Drawable drawable) {
        this.ˍ.setBackgroundDrawable(drawable);
    }

    @Nullable
    public View ॱ() {
        return this.ᐝॱ;
    }

    public void ॱ(@Nullable View view) {
        this.ᐝॱ = view;
    }

    public int ॱॱ() {
        return this.ᐝ;
    }

    public void ˋ(int i) {
        this.ᐝ = i;
    }

    public int ʼ() {
        if (this.ॱॱ) {
            return this.ʻ;
        }
        return 0;
    }

    public void ˎ(int i) {
        this.ʻ = i;
        this.ॱॱ = true;
    }

    public int ʻ() {
        return this.ˏ;
    }

    public void ॱ(int i) {
        this.ˏ = i;
    }

    public void ˊ(int i) {
        Drawable background = this.ˍ.getBackground();
        if (background != null) {
            background.getPadding(this.ˉ);
            this.ˏ = (this.ˉ.left + this.ˉ.right) + i;
            return;
        }
        ॱ(i);
    }

    public void ˋ(@Nullable OnItemClickListener onItemClickListener) {
        this.ʿ = onItemClickListener;
    }

    public void ˊ() {
        boolean z = false;
        int i = -1;
        int ˋॱ = ˋॱ();
        boolean ˏॱ = ˏॱ();
        ReflectUtil.callMethod(this.ˍ, "setAllowScrollingAnchorParent", new Class[]{Boolean.TYPE}, new Object[]{Boolean.valueOf(ˏॱ)}, PopupWindow.class);
        if (VERSION.SDK_INT >= 23) {
            this.ˍ.setWindowLayoutType(this.ʽ);
        }
        int i2;
        if (!this.ˍ.isShowing()) {
            if (this.ˏ == -1) {
                i2 = -1;
            } else if (this.ˏ == -2) {
                i2 = ॱ().getWidth();
            } else {
                i2 = this.ˏ;
            }
            if (this.ˊ == -1) {
                ˋॱ = -1;
            } else if (this.ˊ != -2) {
                ˋॱ = this.ˊ;
            }
            this.ˍ.setWidth(i2);
            this.ˍ.setHeight(ˋॱ);
            ReflectUtil.callMethod(this.ˍ, "setClipToScreenEnabled", new Class[]{Boolean.TYPE}, new Object[]{Boolean.valueOf(true)}, PopupWindow.class);
            PopupWindow popupWindow = this.ˍ;
            boolean z2 = (this.ॱˊ || this.ͺ) ? false : true;
            popupWindow.setOutsideTouchable(z2);
            this.ˍ.setTouchInterceptor(this.ʼॱ);
            ReflectUtil.callMethod(this.ˍ, "setEpicenterBounds", new Class[]{Rect.class}, new Object[]{this.ˊˋ}, PopupWindow.class);
            if (this.ˊॱ && VERSION.SDK_INT >= 23) {
                this.ˍ.setOverlapAnchor(this.ʼ);
            }
            this.ˍ.showAsDropDown(ॱ(), this.ᐝ, this.ʻ, this.ˏॱ);
            this.ˎ.setSelection(-1);
            this.ˍ.getContentView().requestFocus(130);
            if (!this.ˋᐝ || this.ˎ.isInTouchMode()) {
                ʽ();
            }
            if (!this.ˋᐝ) {
                this.ˋˊ.post(this.ˊˊ);
            }
        } else if (ॱ().isAttachedToWindow()) {
            int i3;
            int i4;
            if (this.ˏ == -1) {
                i3 = -1;
            } else if (this.ˏ == -2) {
                i3 = ॱ().getWidth();
            } else {
                i3 = this.ˏ;
            }
            if (this.ˊ == -1) {
                if (ˏॱ) {
                    ˋॱ = -1;
                }
                PopupWindow popupWindow2;
                if (ˏॱ) {
                    popupWindow2 = this.ˍ;
                    if (this.ˏ == -1) {
                        i2 = -1;
                    } else {
                        i2 = 0;
                    }
                    popupWindow2.setWidth(i2);
                    this.ˍ.setHeight(-1);
                    i4 = ˋॱ;
                } else {
                    popupWindow2 = this.ˍ;
                    if (this.ˏ == -1) {
                        i2 = -1;
                    } else {
                        i2 = 0;
                    }
                    popupWindow2.setWidth(i2);
                    this.ˍ.setHeight(0);
                    i4 = ˋॱ;
                }
            } else if (this.ˊ == -2) {
                i4 = ˋॱ;
            } else {
                i4 = this.ˊ;
            }
            PopupWindow popupWindow3 = this.ˍ;
            if (!(this.ॱˊ || this.ͺ)) {
                z = true;
            }
            popupWindow3.setOutsideTouchable(z);
            popupWindow3 = this.ˍ;
            View ॱ = ॱ();
            ˋॱ = this.ᐝ;
            int i5 = this.ʻ;
            if (i3 < 0) {
                i3 = -1;
            }
            if (i4 >= 0) {
                i = i4;
            }
            popupWindow3.update(ॱ, ˋॱ, i5, i3, i);
            this.ˍ.getContentView().requestFocus(130);
        }
    }

    public void ˋ() {
        this.ˍ.dismiss();
        ॱˊ();
        this.ˍ.setContentView(null);
        this.ˎ = null;
        this.ˋˊ.removeCallbacks(this.ʽॱ);
    }

    private void ॱˊ() {
        if (this.ॱˎ != null) {
            ViewParent parent = this.ॱˎ.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(this.ॱˎ);
            }
        }
    }

    public void ʻ(int i) {
        this.ˍ.setInputMethodMode(i);
    }

    public void ʼ(int i) {
        fpt o_fpt = this.ˎ;
        if (ˎ() && o_fpt != null) {
            o_fpt.setListSelectionHidden(false);
            o_fpt.setSelection(i);
            if (o_fpt.getChoiceMode() != 0) {
                o_fpt.setItemChecked(i, true);
            }
        }
    }

    public void ʽ() {
        fpt o_fpt = this.ˎ;
        if (o_fpt != null) {
            o_fpt.setListSelectionHidden(true);
            ReflectUtil.callMethod(o_fpt, "hideSelector", null, null, ListView.class);
            o_fpt.requestLayout();
        }
    }

    public boolean ˎ() {
        return this.ˍ.isShowing();
    }

    public boolean ˏॱ() {
        return this.ˍ.getInputMethodMode() != 2;
    }

    @Nullable
    public fpt ˊॱ() {
        return this.ˎ;
    }

    @NonNull
    fpt ˎ(Context context, boolean z) {
        return new fpt(context, z);
    }

    private int ˋॱ() {
        int i;
        int i2;
        int i3;
        boolean z;
        int i4 = 0;
        LayoutParams layoutParams;
        View view;
        if (this.ˎ == null) {
            Context context = this.ˋ;
            this.ˊᐝ = new Runnable(this) {
                final /* synthetic */ HwListPopupWindow ˎ;

                {
                    this.ˎ = r1;
                }

                public void run() {
                    View ॱ = this.ˎ.ॱ();
                    if (ॱ != null && ॱ.getWindowToken() != null) {
                        this.ˎ.ˊ();
                    }
                }
            };
            this.ˎ = ˎ(context, !this.ˋᐝ);
            if (this.ॱˋ != null) {
                this.ˎ.setSelector(this.ॱˋ);
            } else {
                this.ˎ.setSelector(o.fqk.d.ʽ);
            }
            this.ˎ.setSelectorDrawables(this.ˈ);
            this.ˎ.setAdapter(this.ॱ);
            this.ˎ.setOnItemClickListener(this.ʿ);
            this.ˎ.setFocusable(true);
            this.ˎ.setFocusableInTouchMode(true);
            this.ˎ.setDivider(fo.ˊ(this.ˋ, o.fqk.d.ॱ));
            this.ˎ.setDividerHeight(this.ˋ.getResources().getDimensionPixelSize(o.fqk.b.ˏ));
            this.ˎ.setOnItemSelectedListener(new OnItemSelectedListener(this) {
                final /* synthetic */ HwListPopupWindow ˋ;

                {
                    this.ˋ = r1;
                }

                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long j) {
                    if (i != -1) {
                        fpt ˊ = this.ˋ.ˎ;
                        if (ˊ != null) {
                            ˊ.setListSelectionHidden(false);
                        }
                    }
                }

                public void onNothingSelected(AdapterView<?> adapterView) {
                }
            });
            this.ˎ.setOnScrollListener(this.ʾ);
            View view2 = this.ˎ;
            View view3 = this.ॱˎ;
            if (view3 != null) {
                View linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(1);
                ViewGroup.LayoutParams layoutParams2 = new LayoutParams(-1, 0, 1.0f);
                switch (this.ʻॱ) {
                    case 0:
                        linearLayout.addView(view3);
                        linearLayout.addView(view2, layoutParams2);
                        break;
                    case 1:
                        linearLayout.addView(view2, layoutParams2);
                        linearLayout.addView(view3);
                        break;
                }
                if (this.ˏ >= 0) {
                    i = this.ˏ;
                    i2 = Integer.MIN_VALUE;
                } else {
                    i2 = 0;
                    i = 0;
                }
                view3.measure(MeasureSpec.makeMeasureSpec(i, i2), 0);
                layoutParams = (LayoutParams) view3.getLayoutParams();
                i2 = layoutParams.bottomMargin + (view3.getMeasuredHeight() + layoutParams.topMargin);
                view = linearLayout;
            } else {
                view = view2;
                i2 = 0;
            }
            this.ˍ.setContentView(view);
            i = i2;
        } else {
            view = this.ॱˎ;
            if (view != null) {
                layoutParams = (LayoutParams) view.getLayoutParams();
                i = layoutParams.bottomMargin + (view.getMeasuredHeight() + layoutParams.topMargin);
            } else {
                i = 0;
            }
        }
        Drawable background = this.ˍ.getBackground();
        if (background != null) {
            background.getPadding(this.ˉ);
            i2 = this.ˉ.top + this.ˉ.bottom;
            if (this.ॱॱ) {
                i3 = i2;
            } else {
                this.ʻ = -this.ˉ.top;
                i3 = i2;
            }
        } else {
            this.ˉ.setEmpty();
            i3 = 0;
        }
        if (this.ˍ.getInputMethodMode() == 2) {
            z = true;
        } else {
            z = false;
        }
        if (VERSION.SDK_INT >= 24) {
            i2 = this.ˍ.getMaxAvailableHeight(ॱ(), this.ʻ, z);
        } else {
            i2 = this.ˍ.getMaxAvailableHeight(ॱ(), this.ʻ);
        }
        if (this.ͺ || this.ˊ == -1) {
            return i2 + i3;
        }
        int makeMeasureSpec;
        switch (this.ˏ) {
            case -2:
                makeMeasureSpec = MeasureSpec.makeMeasureSpec(this.ˋ.getResources().getDisplayMetrics().widthPixels - (this.ˉ.left + this.ˉ.right), Integer.MIN_VALUE);
                break;
            case -1:
                makeMeasureSpec = MeasureSpec.makeMeasureSpec(this.ˋ.getResources().getDisplayMetrics().widthPixels - (this.ˉ.left + this.ˉ.right), 1073741824);
                break;
            default:
                makeMeasureSpec = MeasureSpec.makeMeasureSpec(this.ˏ - (this.ˉ.left + this.ˉ.right), 1073741824);
                break;
        }
        Object callMethod = ReflectUtil.callMethod(this.ˎ, "measureHeightOfChildren", new Class[]{Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE}, new Object[]{Integer.valueOf(makeMeasureSpec), Integer.valueOf(0), Integer.valueOf(-1), Integer.valueOf(i2 - i), Integer.valueOf(-1)}, ListView.class);
        if (callMethod != null) {
            i4 = ((Integer) callMethod).intValue();
        }
        if (i4 > 0) {
            i += (this.ˎ.getPaddingTop() + this.ˎ.getPaddingBottom()) + i3;
        }
        return i4 + i;
    }
}
